package com.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.southwind.entity.User;
import com.southwind.form.RegisterForm;
import com.southwind.mapper.UserMapper;
import com.southwind.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private UserMapper userMapper;

//    注册
    @Override
    public boolean register(RegisterForm registerForm) {
        // 检查手机号是否已存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", registerForm.getPhone());
        if (userMapper.selectCount(queryWrapper) > 0) {
            return false; // 手机号已存在
        }

        // 创建新用户
        User user = new User();
        user.setUsername(registerForm.getUsername()); // 👈 新增用户名字段
        user.setPhone(registerForm.getPhone());
        user.setPassword(registerForm.getPassword()); // 生产环境应加密存储密码
        userMapper.insert(user);
        return true;
    }
}
